﻿using Camera_Coder;   //摄像头读码器使用
using HalconDotNet;   //Halcon图像处理使用

namespace TestFireBroadcastingHostPhone
{
    public class Camera_Coder
    {
        /// <summary>
        /// 打开相机
        /// </summary>
        /// <returns>返回错误信息</returns>
        public static string Open_Camera()
        {
            string error_inf = "";

            Coder.Open_Set_Camera(out error_inf);   //打开并设置相机

            return error_inf;
        }


        /// <summary>
        /// 关闭相机
        /// </summary>
        /// <returns>返回错误信息</returns>
        public static string Close_Camera()
        {
            string error_inf = "";

            Coder.Close_Camera(out error_inf);    //关闭相机

            return error_inf;
        }


        /// <summary>
        /// 条码自动读取
        /// </summary>
        /// <param name="coder_data">返回条码内容</param>
        /// <returns>返回错误信息</returns>
        public static string Coder_Auto_Read(out string coder_data)
        {
            string error_inf = "";     //错误信息
            coder_data = "";           //读取的条码内容

            string[] qr_code = new string[0];
            string[] code128_code = new string[0];
            HObject Raw_Image = null;


            //打开相机
            error_inf = Open_Camera();          //打开相机
            if (error_inf != "") goto end_sub;


            //读取条码
            error_inf = Coder.Start_Coder(0, 1, out qr_code, out code128_code, out Raw_Image, true);   //开始解码函数,自动调整曝光时间
            if (error_inf != "") goto end_sub;
            else coder_data = code128_code[0];    //获取读到的一维码


            end_sub:
            #region 关闭相机
            if(true)
            {
                string error = Close_Camera();
            }
            #endregion

            return error_inf;          //返回结果
        }

    }
}
